package com.hiyee.admin.controller;

import com.hiyee.admin.service.FriendLinkService;
import com.hiyee.api.BaseController;
import com.hiyee.api.controller.admin.FriendLinkControllerApi;
import com.hiyee.grace.result.ResponseJsonResult;
import com.hiyee.pojo.bo.UpdateFriendLinkBO;
import com.hiyee.pojo.mo.FriendLinkMO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class FriendLinkController extends BaseController implements FriendLinkControllerApi {

    final static Logger logger = LoggerFactory.getLogger(FriendLinkController.class);

    @Autowired
    FriendLinkService friendLinkService;

    @Override
    public ResponseJsonResult update(UpdateFriendLinkBO bo, BindingResult bindingResult) {
        // 校验参数
        if(bindingResult.hasErrors()) {
            return ResponseJsonResult.errorMap(this.getErrors(bindingResult));
        }
        FriendLinkMO friendLinkMO = new FriendLinkMO();
        BeanUtils.copyProperties(bo, friendLinkMO);
        friendLinkMO.setCreatedAt(new Date());
        friendLinkMO.setUpdatedAt(friendLinkMO.getCreatedAt());

        friendLinkService.save(friendLinkMO);
        return ResponseJsonResult.success();
    }

    @Override
    public ResponseJsonResult update() {
        return ResponseJsonResult.success(friendLinkService.getList());
    }

    @Override
    public ResponseJsonResult delete(String link_id) {
        System.out.println(link_id);
        return ResponseJsonResult.success(friendLinkService.delete(link_id));
    }
}
